package com.group3.merchant.listener;

import java.util.HashMap;
import java.util.Map;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

import com.group3.entity.AMerchant;
import com.group3.merchant.utils.JsonUtil;

/**
 * Application Lifecycle Listener implementation class ContextListener
 * 
 */
public class ContextListener implements ServletContextListener {

	Session session = null;
	MessageConsumer consumer = null;
	Connection conn = null;

	/**
	 * Default constructor.
	 */
	public ContextListener() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see ServletContextListener#contextInitialized(ServletContextEvent)
	 */
	public void contextInitialized(ServletContextEvent sce) {
		// TODO Auto-generated method stub
		// 获取connectionFactory工厂，通过其实现类ActiveMQConnectionFactory
		final ServletContext ctx = sce.getServletContext();
		// System.out.println("aaaa");
		ConnectionFactory cf = new ActiveMQConnectionFactory(
				"failover://tcp://localhost:61616");
		// 获取

		try {
			conn = cf.createConnection();
		} catch (JMSException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		if (conn != null) {
			try {
				session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
				Destination queue = new ActiveMQQueue("Admin-Merchant52");
				consumer = session.createConsumer(queue);

				conn.start();
				consumer.setMessageListener(new MessageListener() {

					public void onMessage(Message msg) {
						TextMessage tm = (TextMessage) msg;
						String readText;
						try {
							readText = tm.getText();
							System.out.println("MQ:" + readText);
							AMerchant aMerchant = JsonUtil.getInstance()
									.getObjectFromJson(readText,
											AMerchant.class);

							if (aMerchant != null) {
								String mid = aMerchant.getmId();
								String atype = aMerchant.getType();
								String astatus = aMerchant.getStatus();

								System.out.println("Listener amerchant: "
										+ aMerchant);
								System.out.println("Listener astatus: "
										+ astatus);
								System.out.println("Listener mid: " + mid);

								// if ("black".equals(astatus)) {
								// ctx.getRequestDispatcher("reject.jsp");
								// }
								Map<String, String> amap = new HashMap<>();
								amap.put("mid", mid);
								amap.put("mtype", atype);
								amap.put("mstatus", astatus);
								ctx.setAttribute(mid, amap);
							}
						} catch (JMSException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
				System.out.println("init JMS...");
			} catch (JMSException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}

	}

	/**
	 * @see ServletContextListener#contextDestroyed(ServletContextEvent)
	 */
	public void contextDestroyed(ServletContextEvent sce) {
		// TODO Auto-generated method stub
		System.out.println("destroy");
		if (session == null) {
			try {
				session.close();
			} catch (JMSException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (consumer == null) {
			try {
				consumer.close();
			} catch (JMSException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (conn == null) {
			try {
				conn.close();
			} catch (JMSException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}
